namespace Domotica.Windows.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Input.StylusPlugIns;
    using System.Windows.Media;

    public class CustomDynamicRenderer : DynamicRenderer
    {
        [ThreadStatic]
        private static Brush brush = null;
        [ThreadStatic]
        private static Pen pen = null;
        private Point prevPoint;

        protected override void OnDraw(DrawingContext drawingContext, StylusPointCollection stylusPoints, Geometry geometry, Brush fillBrush)
        {
            if (brush == null)
            {
                brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 20.0);
            }
            if (pen == null)
            {
                pen = new Pen(brush, 2.0);
            }
            int count = stylusPoints.Count - 1;
            for (int i = 0; i <= count; i++)
            {
                Point pt = (Point) stylusPoints[i];
                if (Point.Subtract(this.prevPoint, pt).Length > 4.0)
                {
                    StylusPoint stylusPoint = stylusPoints[i];
                    double radius = stylusPoint.PressureFactor * 10.0;
                    drawingContext.DrawEllipse(brush, pen, pt, radius, radius);
                    this.prevPoint = pt;
                }
            }
        }

        protected override void OnStylusDown(RawStylusInput rawStylusInput)
        {
            this.prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
            base.OnStylusDown(rawStylusInput);
        }
    }
}

